package com.znsd.study.controllor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.internet.AddressException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.znsd.entitys.BlogsInfo;
import com.znsd.entitys.Blogs_ReplyInfo;
import com.znsd.entitys.Reply;
import com.znsd.entitys.SpecialtyInfo;
import com.znsd.entitys.Users;
import com.znsd.study.service.BlogService;
import com.znsd.study.service.BlogsReplyService;
import com.znsd.utils.EmailUtils;

@Controller
public class BlogsController {
	
	@Autowired
	private BlogService blogService;
	@Autowired
	private BlogsReplyService blogsReplyService;
	
	private static int  page=1;
	private static int prpage=0;
	
	@ResponseBody
	@RequestMapping(value = "loadtype",method = RequestMethod.POST)
	public List<SpecialtyInfo> type() {
		System.out.println("123_________________________________________________________");
		List<SpecialtyInfo> list = blogsReplyService.fulist();
		System.out.println(list);
		return list;
	}
	@RequestMapping(value = "tablogs",method = RequestMethod.GET)
	public String taBlogs(String userid,HttpServletRequest request) {
		HttpSession session = request.getSession();
		System.out.println(userid);
		List<BlogsInfo> list = blogService.queryAll("0", userid, null, 100);
		System.out.println(list);
		session.setAttribute("tablogs", list);
		request.setAttribute("ta",list.get(0).getUser_id().getUser_username());
		return "blogs/tablogs";
	}
	
	@RequestMapping(value = "myblogs",method = RequestMethod.GET)
	public String myBlogs(HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		Object obj = session.getAttribute("list");
			List<BlogsInfo> list = blogService.queryAll("0",null,null, 100);
			session.setAttribute("list",list);
			page+=1;
			if(obj==null) {
		}
		return "blogs/blogs";
	}
	
	@RequestMapping(value = "gustbook",method = RequestMethod.GET)
	public String gustbook() {
		return "blogs/gustbook";
	}
	@RequestMapping(value = "update",method = RequestMethod.GET)
	public String update() {
		return "blogs/update";
	}
	@RequestMapping(value = "archives",method = RequestMethod.GET)
	public String archives() {
		return "blogs/archives";
	}
	@RequestMapping(value = "link",method = RequestMethod.GET)
	public String link() {
		return "blogs/link";
	}
	
	@ResponseBody
	@RequestMapping(value = "addblogs" , method = RequestMethod.POST)
	public Boolean addblogs(String blogs,String state,String title,String type,HttpServletRequest request) {
		Users user=(Users) request.getSession().getAttribute("user");
		int con = blogService.addBolgs(title, blogs, type, state, user.getUser_id()+"");
		System.out.println(con);
		if(con>=1) {
			return true;
		}else {
			return false;
		}
		
	}
	
	@RequestMapping(value="success")
	public String success() {
		return "blogs/success";
	}
	@ResponseBody
	@RequestMapping(value = "loadblogs",method = RequestMethod.POST)
	public String loadblogs(HttpServletRequest request) {
		int size=page*10;
		System.out.println(size);
		List<BlogsInfo> list = blogService.queryAll("0",null,null, size);
		HttpSession session = request.getSession();
		session.setAttribute("list",list);
		long count=blogService.queryCount();
		if(size>=count) {
			return "false";
		}
		page+=1;
		return "true";
	}
	
	@RequestMapping(value = "checkblogs",method = RequestMethod.GET)
	public String checkblogs(String blogs_id,HttpServletRequest request) {
		HttpSession session = request.getSession();
		Users user = (Users) session.getAttribute("user");
		
		BlogsInfo blogs = blogService.queryOne(blogs_id);
		request.setAttribute("blogs",blogs);
		String dz=blogs.getBlogs_dz();
		if(dz==null) {
			request.setAttribute("dz", false);
		}else {
			String[] user_id = dz.split(",");
			for (String string : user_id) {
				if(string.equals(user.getUser_id()+"")) {
					request.setAttribute("dz", true);
				}
			}
		}
		//if(session.getAttribute("reply")==null) {
			
			List<Blogs_ReplyInfo> list = blogsReplyService.queryAll(Integer.parseInt(blogs_id),0);
			prpage=1;
			System.out.println("呜啦啦啦");
			session.setAttribute("reply",list);
			
		//}
		return "blogs/checkblogs";
	}
	
	@ResponseBody
	@RequestMapping(value = "dz",method = RequestMethod.POST)
	public String dz(String id,HttpServletRequest request,HttpServletResponse response) {
		HttpSession session = request.getSession();
		Users user = (Users) session.getAttribute("user");
		
		
		BlogsInfo blogs = blogService.queryOne(id);
		String dz=blogs.getBlogs_dz();
		if(dz==null) {
			blogs.setBlogs_dz(user.getUser_id()+",");
			blogs.setBlogs_dzcount(1);
			blogService.updateDz(blogs);
			return "true";
		}else {
			String[] user_id = dz.split(",");
			for (String string : user_id) {
				if(string.equals(user.getUser_id()+"")) {
					blogs.setBlogs_dz( dz.replace(user.getUser_id()+",", ""));
					blogs.setBlogs_dzcount(blogs.getBlogs_dzcount()-1);
					blogService.updateDz(blogs);
					return "false";
				}
			}
			blogs.setBlogs_dz(dz+user.getUser_id()+",");
			blogs.setBlogs_dzcount(blogs.getBlogs_dzcount()+1);
			blogService.updateDz(blogs);
			return "true";
		}
		
	}
	
	
	@RequestMapping(value = "myblogs1",method = RequestMethod.GET)
	public String blogs(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Users user = (Users) session.getAttribute("user");
		List<BlogsInfo> list = blogService.queryAll(null, user.getUser_id()+"", null, 100);
		session.setAttribute("myblogs", list);
		return "blogs/myblogs";
	}
	
	@ResponseBody
	@RequestMapping(value = "delblogs",method =RequestMethod.DELETE )
	public String delblogs(String id,HttpServletRequest request) {
		HttpSession session = request.getSession();
		Users user = (Users) session.getAttribute("user");
		
		System.out.println(id);
		blogService.delBlogs(id);
		/*
		 * List<BlogsInfo> list = blogService.queryAll("0", user.getUser_id()+"", null,
		 * 100); session.setAttribute("myblogs", list);
		 */
		session.removeAttribute("myblogs");
		session.removeAttribute("list");
		//session.setAttribute("list", list);
		return "cg";
	}
	@ResponseBody
	@RequestMapping(value = "reply",method =RequestMethod.POST )
	public String reply(String id,String blogs,HttpServletRequest request) {
		HttpSession session = request.getSession();
		Users user = (Users) session.getAttribute("user");
		System.out.println(id);
		System.out.println(blogs);
		blogsReplyService.addReply(id,user.getUser_id(),blogs,null);
		return "cg";
	}
	
	//分页
	@ResponseBody
	@RequestMapping(value = "replypage",method =RequestMethod.POST )
	public Map<String, Object> replypage(String blogs_id,String state,HttpServletRequest request) {
		HttpSession session = request.getSession();
		Users user = (Users) session.getAttribute("user");
		Map<String, Object> map=new HashMap<String, Object>();
		List<Blogs_ReplyInfo> list;
		if(state.equals("up")) {
			prpage=prpage-1;
			if(prpage<=0) {
				prpage=1;
			}
			int size=(prpage-1)*10;
			System.out.println(size+"up");
			 list = blogsReplyService.queryAll(Integer.parseInt(blogs_id), size);
			session.setAttribute("reply",list);
			map.put("reply", list);
		}else {
			long count = blogsReplyService.queryCount(blogs_id);
			prpage=prpage+1;
			if(prpage>=count/10+1) {
				prpage=(int)count/10+1;
				map.put("msg", "sb");
			}
			int size=(prpage-1)*10;
			System.out.println(size+"next");
			list = blogsReplyService.queryAll(Integer.parseInt(blogs_id), size);
			session.setAttribute("reply",list);
			map.put("reply",list);
		}
		StringBuffer str=new StringBuffer();
		for (Blogs_ReplyInfo br : list) {
			//str.append("<c:forEach var='reply' items='${ reply }'>");
			str.append("<tr style='border-bottom: 1px solid gray;'>");
			str.append("<td width='100px' style='text-align: right;'>");
			str.append("<a style='text-decoration: underline;color: blue;' href='javascript:void(0)' onclick='openw("+br.getUser_id().getUser_id()+")'>"+br.getUser_id().getUser_username()+"</a>说:");
			str.append("</td>");
			str.append("<td>");
			str.append(br.getBlogs_comment());
			str.append("</td>");
			str.append("<td width='150px'>");
			str.append("<a style='color:navy;' href='javascript:void(0)' onclick='hf()''>回复</a>&nbsp;&nbsp;&nbsp;&nbsp;");
			System.out.println(user.getUser_id()+"--------"+br.getUser_id().getUser_id());
			if(user.getUser_id().equals(br.getUser_id().getUser_id())) {
				System.out.println("---------------------");
				str.append("<a class='sc' href='javascript:void(0)' onclick='del("+br.getBr_id()+")'>删除</a>");
			}
			str.append("&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' onclick='zk("+br.getBr_id()+")'>展开</a>");
			str.append("</td>");
			str.append("</tr>");
			//str.append("</c:forEach>");
		}
		map.put("str", str);
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value = "delreply",method = RequestMethod.DELETE)
	public String delreply(String id) {
		int index = blogsReplyService.delrRply(id);
		return index+"";
	}
	
	
	@ResponseBody
	@RequestMapping(value = "reply1",method =RequestMethod.POST )
	public String reply1(String rid,String blogs,String br_id,HttpServletRequest request) {
		HttpSession session = request.getSession();
		Users user = (Users) session.getAttribute("user");
		System.out.println(blogs);
		System.out.println(rid);
		int con;
		if(br_id==null) {
			con = blogsReplyService.addReply1(rid, user, rid, blogs);
		}else {
			con=blogsReplyService.addReply2(rid, user, br_id, blogs);
		}
		return con+"";
	}
	@ResponseBody
	@RequestMapping(value = "unfold",method =RequestMethod.POST )
	public Map<String, Object> unfold(String id,HttpServletRequest request,HttpServletResponse res) {
		Map<String, Object> map=new HashMap<String, Object>();
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/html;charset=utf-8"); 
		HttpSession session = request.getSession();
		Users user = (Users) session.getAttribute("user");
//		System.out.println(id);
		List<Reply> list = blogsReplyService.queryBrid(id);
		StringBuffer str=new StringBuffer();
		str.append("<table id='mypl3'  width='600px' border='0px' style='text-align: left;text-indent: 10px;'>");
		for (Reply reply : list) {
			str.append("<tr style='border-bottom: 1px solid gray;'>");
			str.append("<td width='150px' style='text-align: right;'>");
			str.append("<a style='text-decoration: underline;color: blue;' href='javascript:void(0)' onclick='openw("+reply.getR_users().getUser_id()+")'>"+reply.getR_users().getUser_username()+"</a>回复"+reply.getBr_users().getUser_username()+"说:");
			str.append("</td>");
			str.append("<td>");
			str.append(reply.getR_context());
			str.append("</td>");
			str.append("<td width='150px'>");
			str.append("<a style='color:navy;' href='javascript:void(0)' onclick='hf1("+reply.getBr_id()+","+reply.getR_users().getUser_id()+")'>回复</a>&nbsp;&nbsp;&nbsp;&nbsp;");
			if(user.getUser_id().equals(reply.getR_users().getUser_id())) {
				str.append("<a class='sc' href='javascript:void(0)' onclick='del1("+reply.getR_id()+","+reply.getBr_id()+")'>删除</a>");
			}
			str.append("</td>");
			str.append("</tr>");
		}
		str.append("</table>");
		str.append("<div id='hfpl1' style='display:none;'><input type='text' name='context' />&nbsp;&nbsp; <button id='xtj' onclick='xtj()'>提交</button></div>");
		str.append("<span id='br_id' style='display:none;'></span><span id='r_user' style='display:none;'></span>");
		map.put("data",str);
		return map;
	}
	@ResponseBody
	@RequestMapping(value = "del2",method = RequestMethod.DELETE)
	public String del2(String r_id) {
		int index = blogsReplyService.del2(Integer.parseInt(r_id));
		return index+"";
	}
	
	@ResponseBody
	@RequestMapping(value = "sendemail",method = RequestMethod.POST)
	public String sendemail(String id) {
		
		BlogsInfo bl = blogService.queryOne(id);
		String user_mail = bl.getUser_id().getUser_mail();
		String mail="你的博客内容涉嫌违规已被管理员删除";
		try {
			EmailUtils.sendMail(user_mail, mail);
		} catch (AddressException e) {
			e.printStackTrace();
			return "sb";
		}
		return "cg";
	}
}
